var table;
$(function(){
    table=$("#parameter").DataTable({
        "ajax": {
            url:"../../admin/parameter/Get",
            type:"POST",
            data:function(d){
                d.para_type=$("#search_type").val()
            },error:function(XMLHttpRequest,textStatus,errorThrown){
                console.log("载入数据出错");
            }
        },
        "bProcessing": true,
        "bServerSide": true,
        "bPaginate": true,
        "bAutoWidth": true,
        "bInfo": true,//页脚信息
        "bFilter": false,//禁用搜索
        "bSort":false,//禁用排序
        "aLengthMenu": [10,20],
        "sPaginationType":'full_numbers',
        "columns": [
            {"data":"id"},
            { "data": "para_type" },
            { "data": "para_order" },
            { "data": "para_value" },
            { "data": "id" },
        ],
        "aoColumnDefs": [ {
            "aTargets": [ 1 ],
            "mRender": function ( data, type, full ) {
                switch (data){
                    case 1:return "交易品种";break;
                    case 2:return "部门类型";break;
                    case 3:return "职务类型";break;
                    default :return "岗位性质";break;
                }
            }
        },
            {
                "aTargets": [ 4 ],
                "mRender": function ( data, type, full ) {
                    return '<a onclick="editData(\''+full.id+'\',\''+full.para_type+'\',\''+full.para_order+'\',\''+full.para_value+'\')">修改</a>' +
                        '<a style="margin-left: 12px" onclick="delData(\''+full.id+'\')">删除</a>';
                }
            },
        ],
        "fnRowCallback": function (nRow, aData, iDisplayIndex) {
            $("td:first", nRow).html(iDisplayIndex + 1);
            return nRow;
        },
        "oLanguage": {
            "sProcessing": "处理中...",
            "sLengthMenu": "每页 _MENU_ 条",
            "sZeroRecords": "没有匹配结果",
            "sInfo": " _START_ 至 _END_ 项结果，共 _TOTAL_ 项",//显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项
            "sInfoEmpty": "",//显示第 0 至 0 项结果，共 0 项
            "sInfoFiltered": "",//(由 _MAX_ 项结果过滤)
            "sInfoPostFix": "",
            "sSearch": "搜索：",
            "sUrl": "",
            "sEmptyTable": "没有数据呀!",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页"
            }
        },
    });
})

//偏好使用HTML
new SMValidator('#parameterFrom');

function check_type(){
    table.draw();
}

function add_type(){
    $(".modal-title").text("基本信息字段枚举值动态配置-新增")
    $("#id").val("")
    $("#add_para_order").attr("readonly","readonly")
    $("#add_para_value").val("")
    $("#add_para_type").find("option[value='"+0+"']").attr("selected",true);
    $("#parameterModal").modal("show")
}

function parameter_sub(){
    if(SMValidator.validate('#parameterFrom')){
        $.ajax({
            url: "../../admin/parameter",
            type: "POST",
            dataType: "json",
            data: $("#parameterFrom").serialize(),
            beforeSend: function () {
            },
            success: function (data) {
                if(data){
                    var msg = ""
                    if($("#id").val()){
                        msg = "修改成功"
                    }else {
                        msg = "新增成功"
                    }
                    Lobibox.notify('success', {
                        msg: msg,
                        sound: false
                    });
                    $("#parameterModal").modal("hide")
                    table.draw();
                }else {
                    console.log(data)
                }
            }, error: function () {
            }
        });
    }
}

function editData(id,para_type,para_order,para_value){
    $(".modal-title").text("基本信息字段枚举值动态配置-修改")
    $("#id").val(id)
    $("#add_para_order").val(para_order).removeAttr("readonly")
    $("#add_para_value").val(para_value)
    $("#add_para_type").find("option[value='"+para_type+"']").attr("selected",true);
    $("#parameterModal").modal("show")
}

function delData(id){
    Lobibox.confirm({
        msg: "确认要删除?",
        title:"删除",
        iconClass: false,
        callback: function ($this, type, ev) {
            if(type==='yes'){
                $.ajax({
                    url: "../../admin/parameter/"+id+"?_csrf="+$("input[name=_csrf]").val(),
                    type: "DELETE",
                    dataType: "json",
                    success: function (data) {
                        if(data){
                            table.draw();
                            Lobibox.notify('success', {
                                msg: '删除成功',
                                sound: false
                            });
                        }else {
                            Lobibox.notify('error', {
                                msg: '删除失败',
                                sound: false
                            });
                        }
                    }, error: function () {
                    }
                });
            }
        }
    });
}